#include "uart.h"

static mx_uint8_t readl(volatile mx_uint8_pt addr)
{
    return *addr;
}

static void writel(mx_uint8_t v, volatile mx_uint8_pt addr)
{
    *addr = v;
}

void put_string(char* str)
{
    int i;
    while (readl((mx_uint8_pt)PL011_UARTFR) & (1 << PL011_UARTFR_TXFF_BIT));

    for(i = 0; str[i] != 0; i++)
    {
        writel(str[i], (mx_uint8_pt)PL011_UARTDR);
    }
}

void put_char(char c)
{
    while (readl((mx_uint8_pt)PL011_UARTFR) & (1 << PL011_UARTFR_TXFF_BIT));
    writel(c, (mx_uint8_pt)PL011_UARTDR);
}

mx_int16_t get_char ()
{
    mx_int16_t ch = -1;
    if (!(*((mx_uint16_pt)PL011_UARTFR) & UARTFR_RXFE))
    {
        ch = *((mx_uint8_pt)PL011_UARTDR);
    }
    return ch;
}
